package com.iecas.entity;

import cn.hutool.core.util.RandomUtil;
import com.iecas.context.GlobalContext;
import com.iecas.context.ServerContext;
import com.iecas.enums.Direction;
import com.iecas.launch.GameFrame;

import lombok.Data;

import java.awt.*;
import java.util.List;

@Data
public class Tank extends Base implements GameObject {
    private String username;
    private String token;
    private long lastFireTime;
    private static final long FIRE_DELAY = 1000; // 发射延迟(毫秒)

    public static void init(Tank tank) {
        tank.setX(100);
        tank.setY(100);
        tank.setWidth(50);
        tank.setHeight(50);
        tank.setSpeed(15);
        tank.setDirection(Direction.UP);
    }

    public static Tank random() {
        Tank tank = new Tank();
        tank.setX(RandomUtil.randomInt(GameFrame.WIDTH - 50));
        tank.setY(RandomUtil.randomInt(GameFrame.HEIGHT - 50));
        tank.setWidth(30);
        tank.setHeight(30);
        tank.setSpeed(15);
        tank.setDirection(Direction.UP);
        return tank;
    }

    public boolean canFire() {
        long currentTime = System.currentTimeMillis();
        return currentTime - lastFireTime >= FIRE_DELAY;
    }

    public Bullet fire() {
        lastFireTime = System.currentTimeMillis();
        Bullet bullet = null;
        switch (direction) {
            case UP:
                bullet = new Bullet(x + 12, y - 12, 10, 10, direction);
                break;
            case DOWN:
                bullet = new Bullet(x + 12, y + getHeight(), 10, 10, direction);
                break;
            case LEFT:
                bullet = new Bullet(x - 12, y + 12, 10, 10, direction);
                break;
            case RIGHT:
                bullet = new Bullet(x + getWidth(), y + 12, 10, 10, direction);
                break;
        }
        return bullet;
    }

    public void updatePlayer(boolean[] keys, Direction direction) {
        int dx = 0;
        int dy = 0;
        if (Direction.UP.equals(direction)) {
            dy = -3;
        } else if (Direction.RIGHT.equals(direction)) {
            dx = 3;
        } else if (Direction.DOWN.equals(direction)) {
            dy = 3;
        } else if (Direction.LEFT.equals(direction)) {
            dx = -3;
        }
        setDirection(direction);

        // 检查移动是否会导致碰撞
        if (!checkCollision(getX() + dx, getY(), getWidth(), getHeight(), getDirection())) {
            setX(getX() + dx);
        }

        if (!checkCollision(getX(), getY() + dy, getWidth(), getHeight(), getDirection())) {
            setY(getY() + dy);
        }

        // 边界检查
        setX(Math.max(0, Math.min(getX(), GameFrame.WIDTH - 30)));
        setY(Math.max(0, Math.min(getY(), GameFrame.HEIGHT - 30)));

    }

    private boolean checkCollision(int x, int y, int width, int height, Direction direction) {
        Rectangle tankRect = new Rectangle(x, y, width, height);

        ServerContext serverContext = GlobalContext.getServerContext();
        List<Wall> walls = serverContext.getWalls();
        // 检查与墙壁的碰撞
        for (Wall wall : walls) {
            if (tankRect.intersects(wall.getBounds())) {
                return true;
            }
        }
        // 检查与敌人的碰撞（仅玩家需要检查）
//        if (direction == player.getDirection() && x == player.getX() && y == player.getY()) {
//            for (EnemyTank enemy : enemies) {
//                if (enemy.isAlive() && tankRect.intersects(enemy.getBounds())) {
//                    return true;
//                }
//            }
//        }
        return false;
    }

    @Override
    public Rectangle getBounds() {
        return new Rectangle(x, y, width, height);
    }
}
